using System;
using Framework.Library;
using Framework.Library.Components.Modules;
using Framework.Library.Utilities;
using Framework.Module.CMS.Library.Definitions;
using Framework.Module.CMS.Library.Distributions;
using Framework.Module.CMS.Admin.Zones;
using Framework.Library.Components.Portal;
using Framework.Module.CMS.Library.Zones;

namespace Framework.Module.CMS.Library
{
    public class CMSWebControl : ModuleControlBase
    {
        private int zoneTabId;
        private int contentTabId;
        protected ZoneInfo curZone;
        protected DistributionInfo curDistInfo;
        private int portalid;
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            //portalid = ConvertUtility.ToInt32(PortalSetting.GetSetting()[Definition.Portal_Active + "_" + AppEnv.GetLanguage()]);
            portalid = ConvertUtility.ToInt32(AppEnv.PortalId());
            zoneTabId = ConvertUtility.ToInt32(Portal[Definition.Portal_Zone_Tab + "_" + portalid + "_" + AppEnv.GetLanguage()]);
            contentTabId = ConvertUtility.ToInt32(Portal[Definition.Portal_Content_Tab + "_" + portalid + "_" + AppEnv.GetLanguage()]);

            int curZoneId = ConvertUtility.ToInt32(Request.QueryString["catid"]);
            if (Request.QueryString["catid"] != null)
            {
                curZone = ZoneController.GetZone(curZoneId);
            }
            else
            {
                curZone = ZoneController.GetZone(253);
            }
            int distId = ConvertUtility.ToInt32(Request.QueryString["distid"]);
            if (distId != 0)
            {
                curDistInfo = DistributionController.GetDistribution(distId);
                if (curZone == null && curDistInfo != null) curZone = ZoneController.GetZone(curDistInfo.Distribution_ZoneID);
            }
        }

        protected string BuildUrlByZone(int zoneId, int realUrl)
        {
            portalid = ConvertUtility.ToInt32(AppEnv.PortalId());

            if (realUrl == 0)
            {
                return AppEnv.DEFAULT_PAGE + "?portalid=" + portalid + "&tabid=" + zoneTabId + "&catid=" + zoneId;
            }
            else
            {
                return AppEnv.DEFAULT_PAGE + "?portalid=" + portalid + "&tabid=" + realUrl + "&catid=" + zoneId;
            }
            //portalid = ConvertUtility.ToInt32(PortalSetting.GetSetting()[Definition.Portal_Active + "_" + AppEnv.GetLanguage()]);

            //if (realUrl == 0)
            //{
            //    return "/portalid/" + portalid + "/tabid/" + zoneTabId + "/catid/" + zoneId + AppEnv.DEFAULT_EXTENTSION;
            //}
            //else
            //{
            //    return "/portalid/" + portalid + "/tabid/" + realUrl + "/catid/" + zoneId + AppEnv.DEFAULT_EXTENTSION;
            //}
        }
        protected string BuildUrlByZoneSubPortal(int zoneId, int realUrl)
        {
            portalid = ConvertUtility.ToInt32(AppEnv.PortalId());

            if (realUrl == 0)
            {
                return AppEnv.DEFAULT_PAGE + "?portalid=" + portalid + "&tabid=" + zoneTabId + "&catid=" + zoneId;
            }
            else
            {
                return AppEnv.DEFAULT_PAGE + "?portalid=" + portalid + "&tabid=" + realUrl + "&catid=" + zoneId;
            }
            //portalid = ConvertUtility.ToInt32(AppEnv.PortalId());

            //if (realUrl == 0)
            //{
            //    return "/portalid/" + portalid + "/tabid/" + zoneTabId + "/catid/" + zoneId + AppEnv.DEFAULT_EXTENTSION;
            //}
            //else
            //{
            //    return "/portalid/" + portalid + "/tabid/" + realUrl + "/catid/" + zoneId + AppEnv.DEFAULT_EXTENTSION;
            //}
        }
        protected string BuildUrlByDistId(int distId, int zoneId)
        {
            if(this.contentTabId == 0)
                this.contentTabId = ConvertUtility.ToInt32(Portal[Definition.Portal_Content_Tab + "_" + portalid + "_" + AppEnv.GetLanguage()]);

            //return "/portalid/" + portalid + "/tabid/" + contentTabId + "/catid/" + zoneId + "/distid/" + distId + AppEnv.DEFAULT_EXTENTSION;
            return AppEnv.DEFAULT_PAGE + "?portalid=" + portalid + "&tabid=" + contentTabId + "&catid=" + zoneId + "&distid=" + distId;
        }

        protected string BuildUrlByContentTab(int zoneId)
        {            
            int contentTab = ConvertUtility.ToInt32(Portal[Definition.Portal_ContentZone_Tab + "_" + portalid + "_" + AppEnv.GetLanguage()]);
            //return "/portalid/" + portalid + "/tabid/" + contentTab + "/catid/" + zoneId + AppEnv.DEFAULT_EXTENTSION;
            return AppEnv.DEFAULT_PAGE + "?portalid=" + portalid + "&tabid=" + contentTab + "&catid=" + zoneId;
        }

        protected string BuildUrlbyPortal()
        {
            return AppEnv.DEFAULT_PAGE + "?portalid=" + AppEnv.PortalId() + "&tabid=" + ConvertUtility.ToInt32(Portal[Definition.Portal_Home_Tab + "_" + AppEnv.PortalId() + "_" + AppEnv.GetLanguage()]); 
            //return "/portalid/" + AppEnv.PortalId() + "/tabid/" + ConvertUtility.ToInt32(Portal[Definition.Portal_Home_Tab + "_" + AppEnv.PortalId() + "_" + AppEnv.GetLanguage()]) + AppEnv.DEFAULT_EXTENTSION; 
        }

        protected string BuildUrlbyPortal(int _portalid)
        {
            return AppEnv.DEFAULT_PAGE + "?portalid=" + _portalid + "&tabid=" + ConvertUtility.ToInt32(Portal[Definition.Portal_Home_Tab + "_" + _portalid + "_" + AppEnv.GetLanguage()]);
            //return "/portalid/" + _portalid + "/tabid/" + ConvertUtility.ToInt32(Portal[Definition.Portal_Home_Tab + "_" + _portalid + "_" + AppEnv.GetLanguage()]) + AppEnv.DEFAULT_EXTENTSION;
        }

        
        protected string BuildUrlbyEvent()
        {
            return AppEnv.DEFAULT_PAGE + "?portalid=" + AppEnv.PortalId() + "&tabid=" + ConvertUtility.ToInt32(Framework.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Event_Tab + "_" + AppEnv.PortalId() + "_" + AppEnv.GetLanguage()]); ;
            //return "/portalid/" + AppEnv.PortalId() + "/tabid/" + ConvertUtility.ToInt32(Framework.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Event_Tab + "_" + AppEnv.PortalId() + "_" + AppEnv.GetLanguage()]) + AppEnv.DEFAULT_EXTENTSION;
        }

        protected string BuildUrlbyEvent(int eventid)
        {
            return AppEnv.DEFAULT_PAGE + "?portalid=" + AppEnv.PortalId() + "&tabid=" + ConvertUtility.ToInt32(Framework.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_NewsInEvent_Tab + "_" + AppEnv.PortalId() + "_" + AppEnv.GetLanguage()]) + "&eventid=" + eventid;
            //return "/portalid/" + AppEnv.PortalId() + "/tabid/" + ConvertUtility.ToInt32(Framework.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_NewsInEvent_Tab + "_" + AppEnv.PortalId() + "_" + AppEnv.GetLanguage()]) + "/eventid/" + eventid + AppEnv.DEFAULT_EXTENTSION;
        }
        protected string BuildUrlSearch(string key)
        {
            return AppEnv.DEFAULT_PAGE + "?portalid=" + AppEnv.PortalId() + "&tabid=" + ConvertUtility.ToInt32(Framework.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Search_Tab + "_" + AppEnv.PortalId() + "_" + AppEnv.GetLanguage()]) + "&keysearch=" + key;
            return "/portalid/" + AppEnv.PortalId() + "/tabid/" + ConvertUtility.ToInt32(Framework.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Search_Tab + "_" + AppEnv.PortalId() + "_" + AppEnv.GetLanguage()]) + "/keysearch/" + key + AppEnv.DEFAULT_EXTENTSION;
        }

        protected string BuildUrlSearchAdvance(string key, string opt)
        {
            return AppEnv.DEFAULT_PAGE + "?portalid=" + AppEnv.PortalId() + "&tabid=" + ConvertUtility.ToInt32(Framework.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Search_Tab + "_" + AppEnv.PortalId() + "_" + AppEnv.GetLanguage()]) + "&keysearch=" + key;
            //return "/portalid/" + AppEnv.PortalId() + "/tabid/" + ConvertUtility.ToInt32(Framework.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Search_Tab + "_" + AppEnv.PortalId() + "_" + AppEnv.GetLanguage()]) + "/key/" + key + "/opt/" + opt + AppEnv.DEFAULT_EXTENTSION;
        }

        protected string BuildUrlRssAdvance(string key, string opt)
        {
            return AppEnv.DEFAULT_PAGE + "?portalid=" + AppEnv.PortalId() + "&tabid=" + ConvertUtility.ToInt32(Framework.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Search_Tab + "_" + AppEnv.PortalId() + "_" + AppEnv.GetLanguage()]) + "&keysearch=" + key;
            //return "/portalid/" + AppEnv.PortalId() + "/tabid/" + ConvertUtility.ToInt32(Framework.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Rss_Tab + "_" + AppEnv.PortalId() + "_" + AppEnv.GetLanguage()]) + "/key/" + key + "/opt/" + opt + AppEnv.DEFAULT_EXTENTSION;
        }

        protected string BuildUrlByZoneForRss(int zoneId, int realUrl)
        {
           
            return "/portalid/" + portalid + "/tabid/" + ConvertUtility.ToInt32(Framework.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Rss_Tab + "_" + AppEnv.PortalId() + "_" + AppEnv.GetLanguage()]) + "/catid/" + zoneId + AppEnv.DEFAULT_EXTENTSION;
        }

        protected string BuildUrlByEmployment(int zoneId, int empl)
        {
            zoneTabId = ConvertUtility.ToInt32(Portal[Definition.Portal_TuyenDung + "_" + portalid + "_" + AppEnv.GetLanguage()]);
            portalid = ConvertUtility.ToInt32(PortalSetting.GetSetting()[Definition.Portal_Active + "_" + AppEnv.GetLanguage()]);
            return "/portalid/" + portalid + "/tabid/" + zoneTabId + "/catid/" + zoneId + "/employment/" + empl + AppEnv.DEFAULT_EXTENTSION;
        }
        
    }
}
